#include <rtthread.h>
#include <rthw.h>
#include <uc_coding.h>
#include "uc_seg_data_flash_manager.h"


void test_seg_data_flash_manager(void)
{
    int all_num = 5;
    unsigned char end_flag = SEGMENT_WAIT_END;
    char in_data[12] = {"012345678912"};
    unsigned int in_data_len = 12;
    unsigned int out_data_addr;
    unsigned int out_data_len;
    e_segment_parsing_result result;
    unsigned char *mask;
    int mask_len;
    int pos = 0;
    // void *page;
    char re_buf[60] = {0};

    while(1)
    {
        app_ps_header_t ps_header = {0};

        app_set_header_property(PRO_SRC_ADDR, 1, &ps_header.property);
        app_set_header_property(PRO_DEST_ADDR, 1, &ps_header.property);
        app_set_header_property(PRO_PACKET_NUM, 1, &ps_header.property);
        app_set_header_property(PRO_SEGMENT_FLAG, 1, &ps_header.property);

        ps_header.addr.src_addr = 5;
        ps_header.addr.dest_addr = 10;
        ps_header.packet_num = 100;
        ps_header.cmd_type = 1;

        rt_kprintf("test_seg_data_flash_manager start\n");

        flash_segment_packet_init(malloc_stroage_addr,
                free_stroage_addr,
                storage_manager_read,
                storage_manager_write,
                in_data_len);

        for (int n = 1; n <= all_num; n ++)
        {
           if (n == all_num)
                end_flag = SEGMENT_HAVE_END;

           app_set_segment_info(&ps_header, all_num, end_flag, n);

           result = flash_packege_segment_parsing(&ps_header,
                    in_data,
                    in_data_len,
                    &out_data_addr,
                    &out_data_len,
                    RT_NULL,
                    RT_NULL,
                    RT_NULL);

           rt_kprintf("flash_packege_segment_parsing result %d\n", result);

           if(SEGMENT_PARSING_SUCCESS != result)
            {
                int loss_num;
               if (0 == flash_get_loss_block(ps_header.addr.src_addr, ps_header.packet_num, &mask, &pos, &mask_len, &loss_num))
               {
                   rt_kprintf("pos %d, mask_len %d\n", pos, mask_len);
                   rt_kprintf("mask:");
                   for (int m = 0; m < mask_len; m++)
                    {
                        rt_kprintf("0x%x ", mask[m]);
                    }
                   rt_kprintf("\n");
               }
               rt_kprintf("tick %x\n", flash_get_packet_tick(5, 100));
            }
           else
            {
                rt_kprintf("all data of addr 0x%x len %d\n", out_data_addr, out_data_len);
            }
        }

        storage_manager_read(out_data_addr, re_buf,out_data_len);

        rt_kprintf("out_data_addr:");
        for (int n = 0; n < out_data_len; n++)
            rt_kprintf("%c ", re_buf[n]);
        rt_kprintf("\n");

        flash_seg_packet_clear(5, 100);
        rt_kprintf("flash_packet_clear ok\n");
        flash_segment_packet_deinit();

        rt_kprintf("test_seg_data_flash_manager ok\n");
        rt_thread_delay(2000);
     }
}
